<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="线路" prop="lineId">
        <el-select
          v-model="queryParams.lineId"
          placeholder="请选择线路"
          clearable
        >
          <el-option
            v-for="lineOne in lineList"
            :key="lineOne.id"
            :label="lineOne.lineName"
            :value="lineOne.id"
          ></el-option>
        </el-select>
      </el-form-item>



            <el-form-item label="杆号" prop="poleCode">
        <el-input
          v-model="queryParams.poleCode"
          placeholder="请输入杆号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="杆号名称" prop="poleName">
        <el-input
          v-model="queryParams.poleName"
          placeholder="请输入杆号名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="poleLongitude">
        <el-input
          v-model="queryParams.poleLongitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="poleLatitude">
        <el-input
          v-model="queryParams.poleLatitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>


      <el-form-item label="上级杆号" prop="prePoleId">
        <el-select
          v-model="queryParams.prePoleId"
          placeholder="请选择上级杆号"
          clearable
        >
          <el-option
            v-for="prePoleOne in polereList"
            :key="prePoleOne.id"
            :label="prePoleOne.poleName"
            :value="prePoleOne.id"
          ></el-option>
        </el-select>
      </el-form-item>

      
      <el-form-item label="电杆类型" prop="pole_type">
        <el-select
          v-model="queryParams.poleType"
          placeholder="电杆类型"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in pole_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>




      <el-form-item label="告警信息" prop="alarmInformation">
        <el-input
          v-model="queryParams.alarmInformation"
          placeholder="请输入告警信息"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
    
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:pole:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:pole:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:pole:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yanjing:pole:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="poleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="线路编码" align="center" prop="lineCode" />
      <el-table-column label="线路名称" align="center" prop="lineName" />
       <el-table-column label="杆号" align="center" prop="poleCode" />
      <el-table-column label="杆号名称" align="center" prop="poleName" />
      <el-table-column label="网关id" align="center" prop="gatewayId" />
      <el-table-column label="经度" align="center" prop="poleLongitude" />
      <el-table-column label="纬度" align="center" prop="poleLatitude" />
      <el-table-column label="电杆类型" align="center" prop="poleType">
        <template #default="scope">
          <dict-tag :options="pole_type" :value="scope.row.poleType" />
        </template>
      </el-table-column> 
      <el-table-column label="上级杆号" align="center" prop="prePoleCode" />
      <el-table-column label="上级杆号名称" align="center" prop="prePoleName" />
      <el-table-column label="告警信息" align="center" prop="alarmInformation" />
      <el-table-column label="告警状态" align="center" prop="alarmStatus" :formatter="formatterTemperatureStatus"/>
      <el-table-column label="操作" width="200%" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:pole:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="addQueryMessageToTopic(scope.row)"
            v-hasPermi="['yanjing:pole:edit']"
          >下发查询</el-button>
          <el-button
          size="mini"
          type="text"
          icon="edit"
          @click="addMessageToTopic(scope.row)"
          v-hasPermi="['yanjing:pole:edit']"
         >下发配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:pole:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电杆信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">


          <el-form-item label="线路" prop="lineId">
              <el-select
                v-model="form.lineId"
                placeholder="请选择线路"
                clearable
              >
                <el-option
                  v-for="lineOne in lineList"
                  :key="lineOne.id"
                  :label="lineOne.lineName"
                  :value="lineOne.id"
                ></el-option>
              </el-select>
            </el-form-item>



        <el-form-item label="杆号" prop="poleCode">
          <el-input v-model="form.poleCode" placeholder="请输入杆号" />
        </el-form-item>

        <el-form-item label="杆号名称" prop="poleName">
          <el-input v-model="form.poleName" placeholder="请输入杆号名称" />
        </el-form-item>
        <el-form-item label="网关id" prop="gatewayId">
          <el-input v-model="form.gatewayId" placeholder="请输入网关id" />
        </el-form-item>
        <el-form-item label="经度" prop="poleLongitude">
          <el-input v-model="form.poleLongitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="poleLatitude">
          <el-input v-model="form.poleLatitude" placeholder="请输入纬度" />
        </el-form-item>



                    <el-form-item class="zdy" label="电杆类型" prop="poleType">
              <el-select v-model="form.poleType" placeholder="" clearable>
                <el-option
                  v-for="item in pole_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>



          <el-form-item label="上级杆号" prop="prePoleId">
              <el-select
                v-model="form.prePoleId"
                placeholder="请选择上级杆号"
                clearable
              >
                <el-option
                  v-for="prePoleOne in polereList"
                  :key="prePoleOne.id"
                  :label="prePoleOne.poleName"
                  :value="prePoleOne.id"
                ></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="告警信息" prop="alarmInformation">
          <el-input v-model="form.alarmInformation" placeholder="请输入告警信息：告警点位信息组合" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPole, getPole, delPole, addPole, updatePole, selectElectricityPoleAndLineInfoList,addQueryMessageToTopic,addMessageToTopic } from "@/api/line/pole.js";
import { selectLineAndAreaInfoList } from "@/api/line/line.js";

export default {
    setup() {
    // 获取当前组件实例
    const instance = getCurrentInstance();
    const { pole_type } = instance.proxy.useDict("pole_type");
    return {
      pole_type,
    };
  },
  name: "Pole",
  data() {
    return {
      lineList:[],
      polereList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电杆信息表格数据
      poleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lineId: null,
        poleName: null,
        poleLongitude: null,
        poleLatitude: null,
        poleType: null,
        prePoleId: null,
        alarmInformation: null,
        alarmStatus: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createEmployeeId: null,
        updateEmployeeId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.listPole();
    this.getLineList();
    this.getList();
  },
  methods: {
        formatterTemperatureStatus(row, column, cellValue, index) {
      if(row.alarmStatus != null && row.alarmStatus == '0'){
        return "温度异常";
      }else{
        return "温度正常";
      }
    },
    /** 查询线路信息列表 */
    getLineList() {
      selectLineAndAreaInfoList(this.queryParams).then(response => {
        this.lineList = response.rows;
      });
    },
    listPole() {
      listPole(this.queryParams).then(response => {
        this.polereList = response.rows;
      });
    },
    /** 查询电杆信息列表 */
    getList() {
      this.loading = true;
      selectElectricityPoleAndLineInfoList(this.queryParams).then(response => {
        this.poleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        lineId: null,
        poleName: null,
        poleLongitude: null,
        poleLatitude: null,
        poleType: null,
        prePoleId: null,
        alarmInformation: null,
        alarmStatus: null,
        standby1: null,
        standby2: null,
        standb3: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电杆信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      listPole();
      const id = row.id || this.ids
      getPole(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电杆信息";
      });
    },
    addQueryMessageToTopic(row){
        var postReq = {
            gatewayId : row.gatewayId
        };
        addQueryMessageToTopic(postReq).then(response => {
          this.$modal.msgSuccess("下发查询成功，请跳转到<Emqx消息日志>页面查询结果");
        });
    },

    addMessageToTopic(row){
        var postReq = {
            gatewayId : row.gatewayId,
            cmd : "add_sensor"
        };
        addMessageToTopic(postReq).then(response => {
          this.$modal.msgSuccess("下发配置成功，请跳转到<Emqx消息日志>页面查询结果");
        });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePole(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getLineList();
              this.listPole();
            });
          } else {
            addPole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getLineList();
              this.listPole();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电杆信息编号为"' + ids + '"的数据项？').then(function() {
        return delPole(ids);
      }).then(() => {
        this.getList();
            this.listPole();
    this.getLineList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yanjing/pole/export', {
        ...this.queryParams
      }, `pole_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
